<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class City_Model extends CI_Model{
    function __construct()
    {
        parent::__construct();
		$ci = get_instance();
		$ci->load->helper('upload_file');
    }
    function getAllCity($num,$offset){
        $query = $this->db->get('city',$num,$offset);
        return $query->result();  
    } 
     function get_object($id)
    {
        $query = $this->db->get_where('city', array('city_id' =>$id));
        return $query->row();
    }
    
    function delete($id){
          $this->db->where('city_id',$id);
          $query = $this->db->delete('city');
          if($query){
              return true;
          }else{
              return false;
          }
    }
	function deleted($id){
          $this->db->where('city_id',$id);
		  $data=array('bl_active'=>'-1');
          $query = $this->db->update('city',$data);
          if($query){
              return true;
          }else{
              return false;
          }
    }
    function get_list_permit(){
        return $this->db->get('permission')->result();
    }
   
   function saveData($city_id,$data){
             if($city_id!=0){
                    $this->db->where('city_id',$city_id);
                    if($this->db->update('city',$data)){
                         return true;
                    }else{
                         return false;
                    }
             }else{
                    if($this->db->insert('city',$data)){
                        return true;
                        
                    }else{
                        return false;
                    }
            }
    }
    function get_num_data($arr_search){
            /*Begin search*/
            switch($arr_search['field_search'])
            {
                    case 'name':
                            $this->db->like('name',$arr_search['key_search']);
                            break ;
                    
            }
            /*End search*/
             $this->db->where('bl_active <> ',-1);
             $query = $this->db->get('city');
             return $query->num_rows();
    }
    function get_all_data($num,$offset,$arr_search)
    {
            /*Begin search*/
            switch($arr_search['field_search'])
            {
                    case 'name':
                            $this->db->like('name',$arr_search['key_search']);
                            break ;
                    
            }
            /*End search*/
            $this->db->where('bl_active <> ',-1);
            $this->db->order_by('nb_order','DESC');
            $this->db->order_by('name','ASC');
            $query = $this->db->get('city',$num,$offset);
             return $query->result();
    }
		function optionCity($value=0){
			$this->db->order_by('nb_order','DESC');
		   $query = $this->db->get('city');
		   $category_arr = $query->result();
		   $str_opt = "";
		   foreach($category_arr as $item_opt){
				$str_opt .="<option value='".$item_opt->city_id."'".(($value==$item_opt->city_id)?" selected":"")."> ".$item_opt->name."</option>";
			}
			return $str_opt;
	   }
	   function inputAllCity($value=0){
		   $query = $this->db->get('city');
		   $category_arr = $query->result();
		   $str_opt = "";
		   $arr_value_check = explode(',',$value);
		   
		   foreach($category_arr as $item_opt){
			   $input_name=removeSign($item_opt->name);
				$str_opt .="<input type='checkbox' name='".$input_name."' value='".$item_opt->city_id."'".(in_array($item_opt->city_id,$arr_value_check)?" checked":"")." />".$item_opt->name." <br />";
			}
			return $str_opt;
	   }
	
	  
  }
?>
